﻿using System;
using System.Windows;
using System.Windows.Controls;
using TheOliver.DigitalSignage.Client.Interfaces;

namespace TheOliver.DigitalSignage.Client.Windows
{
    public partial class SettingsWindow : ChildWindow
    {
        public SettingsWindow()
        {
            InitializeComponent();

            // 
            // Simulate Date Time
            // 
            if (Configuration.SimulateDateTime != null)
            {
                _simulateDateTime.IsChecked = true;

                // Yes, simulate
                _simulateTime.IsEnabled = true;
                _simulateTime.Value = Configuration.SimulateDateTime;
                _simulateDate.IsEnabled = true;
                _simulateDate.SelectedDate = Configuration.SimulateDateTime.Value.Date;
            }
            else
            {
                _simulateDateTime.IsChecked = false;

                // No, don't simulate
                _simulateTime.IsEnabled = false;
                _simulateDate.IsEnabled = false;
            }

            // 
            // Display Duration
            // 
            _displayDuration.Value = Configuration.DefaultDisplayDuration;

            //
            // AgendaItemDefaultDisplayTimeInMilliSeconds
            // 
            _agendaItemDefaultDisplayTimeInMs.Value = Configuration.AgendaItemDefaultDisplayTimeInMilliSeconds;

            // 
            // AgendaItemDefaultWaitTimeInMilliseconds
            // 
            _agendaWaitTimeInMs.Value = Configuration.AgendaItemDefaultWaitTimeInMilliseconds;

            // 
            // Agenda Item Gap
            // 
            _minAgendaItemGap.Value = Configuration.MinAgendaItemGap;

            // 
            // Data Update Interval
            // 
            _dataUpdateInterval.Value = Configuration.DataUpdateInterval;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            // 
            // Simulate DateTime
            // 
            if (_simulateDateTime.IsChecked.HasValue &&
                    _simulateDateTime.IsChecked.Value)
            {
                // Yes, simulate
                DateTime dt = _simulateDate.SelectedDate.Value;
                TimeSpan ts = new TimeSpan(_simulateTime.Value.Value.TimeOfDay.Ticks);
                dt = dt.Add(ts);
                Configuration.SimulateDateTime = dt;
            }
            else
            {
                // No, don't simulate
                Configuration.SimulateDateTime = null;
            }
            this.DialogResult = true;

            // 
            // Display Duration
            // 
            Configuration.DefaultDisplayDuration =  (int)_displayDuration.Value;

            //
            // AgendaItemDefaultDisplayTimeInMilliSeconds
            // 
            Configuration.AgendaItemDefaultDisplayTimeInMilliSeconds = (int)_agendaItemDefaultDisplayTimeInMs.Value;

            // 
            // AgendaItemDefaultWaitTimeInMilliseconds
            // 
            Configuration.AgendaItemDefaultWaitTimeInMilliseconds = (int)_agendaWaitTimeInMs.Value;

            // 
            // Agenda Item Gap
            // 
            Configuration.MinAgendaItemGap = (int)_minAgendaItemGap.Value;

            // 
            // Data Update Interval
            // 
            Configuration.DataUpdateInterval = (int)_dataUpdateInterval.Value;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void _simulateDateTime_Checked(object sender, RoutedEventArgs e)
        {
            _simulateDate.SelectedDate = DateTime.Today;
            _simulateTime.Value = DateTime.Now;
            _simulateDate.IsEnabled = true;
            _simulateTime.IsEnabled = true;
        }

        private void _simulateDateTime_Unchecked(object sender, RoutedEventArgs e)
        {
            _simulateDate.SelectedDate = null;
            _simulateTime.Value = null;
            _simulateDate.IsEnabled = false;
            _simulateTime.IsEnabled = false;
        }
    }
}

